function pv = pvfix(rate, nper, p, fv, due)
%PVFIX Present value with fixed periodic payments.
%
%   pv = pvfix(RATE, NPER, P)
%   pv = pvfix(RATE, NPER, P, FV, DUE)
%
%   Optional parameters: FV, DUE
%
%   Inputs:
%   RATE - Scalar or vector of periodic interest rate.
%
%   NPER - Scalar or vector of number of periods.
%
%      P - Scalar or vector of periodic payments.
%
%   Optional inputs:
%     FV - Scalar or vector of the payments received other than P in the last
%          period. Default FV = 0.
%
%    DUE - Scalar or vector of the 0's or 1's that specify whether payments
%          are made at the beginning of a period (DUE = 1) or whether payments
%          are made at the end of a period (DUE = 0).  Default DUE = 0.
%
%   Outputs:
%      PV - Scalar or vector of present values.
%
%   Example:
%      $200 payment is made monthly into a savings account earning 6%. The
%      payments are made at the end of the month for 5 years.
%
%      pv = pvfix(.06/12, 5*12, 200, 0, 0)
%      pv = 10345.11
%
%   See also FVFIX, FVVAR , PVVAR, PAYPER.
 
%       Copyright 1995-2006 The MathWorks, Inc.
%       $Revision: 1.6.2.5 $   $Date: 2006/06/16 20:10:25 $ 
 
%---------------------------------------------
% Input validation
%---------------------------------------------
if nargin < 3
    error('Finance:pvfix:notEnoughInputs', 'Too few inputs.')
end

if nargin < 5
    due = zeros(size(p));
end

if nargin < 4
    fv = zeros(size(p));
end

if any(any(due ~= 0 & due ~= 1))
    error('Finance:pvfix:invalidDue', 'DUE must be 0 or 1.')
end

if isempty(rate) || isempty(nper) || isempty(p) || isempty(fv) || isempty(due)
    fv = [];
    return
end

%scalar expansion
[rate,nper,p,fv,due] = finargsz('scalar', rate,nper,p,fv,due); 


% Algorithm
c = 1+rate;
pv = (p.*(1+rate.*due).*(c.^nper-1)./rate+fv)./c.^nper;



